package me.idashu.attg.controller;

import java.io.UnsupportedEncodingException;
import java.util.Locale;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baidu.yun.channel.auth.ChannelKeyPair;
import com.baidu.yun.channel.client.BaiduChannelClient;
import com.baidu.yun.channel.exception.ChannelClientException;
import com.baidu.yun.channel.exception.ChannelServerException;
import com.baidu.yun.channel.model.PushBroadcastMessageRequest;
import com.baidu.yun.channel.model.PushBroadcastMessageResponse;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;

@Controller
@RequestMapping("/push")
public class PushController {

	private final static String API_KEY = "7XgBhmG7zUYBemZO9DDBtLcW";
	private final static String SECRIT_KEY = "HGRk7Lj2YLdMuoOzFyDTiIVKrTADbAfU";

	@RequestMapping(value = "/presend", method = RequestMethod.GET)
	public String presend(Locale locale, Model model){
		
		model.addAttribute("push","推送列表");
		
		return "push";
	}
	
	
	
	@RequestMapping(value = "/sendmsg", method = RequestMethod.POST)
	public String sendmsg(@RequestParam String title, @RequestParam String content,
			Locale locale, Model model) {

		String encodetitle = "";
		String encodecontent = "";
		try {
			encodetitle = new String(title.getBytes("iso-8859-1"), "utf-8");
			encodecontent = new String(content.getBytes("iso-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		System.out.println(title);
		System.out.println(content);
		pushAndroid(title, content);
		pushIOS(title,content);
		

		return "push";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String preadd(Locale locale, Model model) {
		model.addAttribute("first", "管理员信息管理");
		model.addAttribute("second", "消息推送");

		return "feedback/push";
	}

	
	
	public void pushAndroid(String encodetitle,String encodecontent){
		
		/*
		 * @brief	推送广播消息(消息类型为透传，由开发方应用自己来解析消息内容)
		 * 			message_type = 0 (默认为0)
		 */
		ChannelKeyPair pair = new ChannelKeyPair(API_KEY, SECRIT_KEY);
		
		// 2. 创建BaiduChannelClient对象实例
		BaiduChannelClient channelClient = new BaiduChannelClient(pair);
		
		
		
		try {
			
			// 4. 创建请求类对象
			PushBroadcastMessageRequest request = new PushBroadcastMessageRequest();
			request.setDeviceType(3);	// device_type => 1: web 2: pc 3:android 4:ios 5:wp	
			
			//request.setMessage("Hello Channel");
			// 若要通知，
			request.setMessageType(1);
	        request.setMessage("{\"title\":\""+encodetitle+"\",\"description\":\""+encodecontent+"\"}");
 			
			// 5. 调用pushMessage接口
			PushBroadcastMessageResponse response = channelClient.pushBroadcastMessage(request);
				
			// 6. 认证推送成功
			System.out.println("push amount : " + response.getSuccessAmount()); 
			
		} catch (ChannelClientException e) {
			// 处理客户端错误异常
			e.printStackTrace();
		} catch (ChannelServerException e) {
			// 处理服务端错误异常
			System.out.println(
					String.format("request_id: %d, error_code: %d, error_message: %s" ,
						e.getRequestId(), e.getErrorCode(), e.getErrorMsg()
						)
					);
		}
		
	}
	
	
public void pushIOS(String encodetitle,String encodecontent){
		
		/*
		 * @brief	推送广播消息(消息类型为透传，由开发方应用自己来解析消息内容)
		 * 			message_type = 0 (默认为0)
		 */
		ChannelKeyPair pair = new ChannelKeyPair(API_KEY, SECRIT_KEY);
		
		// 2. 创建BaiduChannelClient对象实例
		BaiduChannelClient channelClient = new BaiduChannelClient(pair);
		
		
		
		try {
			
			// 4. 创建请求类对象
			PushBroadcastMessageRequest request = new PushBroadcastMessageRequest();
			request.setDeviceType(4);	
			
			request.setMessageType(1);
	        request.setMessage("{\"title\":\""+encodetitle+"\",\"description\":\""+encodecontent+"\"}");
 			
			// 5. 调用pushMessage接口
			PushBroadcastMessageResponse response = channelClient.pushBroadcastMessage(request);
				
			// 6. 认证推送成功
			System.out.println("push amount : " + response.getSuccessAmount()); 
			
		} catch (ChannelClientException e) {
			// 处理客户端错误异常
			e.printStackTrace();
		} catch (ChannelServerException e) {
			// 处理服务端错误异常
			System.out.println(
					String.format("request_id: %d, error_code: %d, error_message: %s" ,
						e.getRequestId(), e.getErrorCode(), e.getErrorMsg()
						)
					);
		}
		
	}
}
